﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMSABL;
using WMSABL.SY;
using WMSAML.SY;
using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message;

namespace ElectronicWMSA.SY
{
    public partial class DictionaryFrm : Form
    {
        #region
        private int _sortid = 0;
        private int _categoryid = 0;
        private string strWhere = "";
        PublicDataSet bll_Datset = new PublicDataSet();
        TDictionary_SortBLL bll_Sort = new TDictionary_SortBLL();
        TDictionary_CategoryBLL bll_Category = new TDictionary_CategoryBLL();
        /// <summary>
        /// 构造函数
        /// </summary>
        public DictionaryFrm()
        {
            InitializeComponent();
            this.linkLabelClose.BackColor = Color.Transparent;
            this.linkLabelCloseElse.BackColor = Color.Transparent;
            this.linkLabellinkLabelCloseAll.BackColor = Color.Transparent;
        }

        /// <summary>
        /// Load加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DictionaryFrm_Load(object sender, EventArgs e)
        {
            this.pageControlDictionary.PageCurrent = 1;
            this.pageControlDictionary.PageSize = 20;
            this.pageControlDictionary.Bind();
            this.DBind();
        }
        #endregion

        #region 绑定
        public void DBind()
        {
            this.label11.BackColor = Color.Transparent;
            this.label12.BackColor = Color.Transparent;
            this.DBindSort();
            this._sortid = 0;
            this._categoryid = 0;
            this.txtCategoryValue.Text = "";
            this.txtSortValue.Text = "";
            this.labShowCategory.Text = "提示信息！";
            this.labShowSort.Text = "提示信息！";
        }

        private int DBindDictionary()
        {
            Comm.Forms.MsControls.PageData pageData = new Comm.Forms.MsControls.PageData();
            pageData.TableName = "Vlh_Sort_Category";
            pageData.PrimaryKey = "ID";
            pageData.OrderStr = "SortID";
            pageData.PageIndex = this.pageControlDictionary.PageCurrent;
            pageData.PageSize = this.pageControlDictionary.PageSize;
            pageData.QueryCondition = strWhere;
            pageData.QueryFieldName = "*";

            this.pageControlDictionary.bindingSource.DataSource = pageData.QueryDataTable().Tables[0];
            this.pageControlDictionary.bindingNavigator.BindingSource = pageControlDictionary.bindingSource;

            this.dataGridViewSortCategoryList.AutoGenerateColumns = false;
            this.dataGridViewSortCategoryList.DataSource = this.pageControlDictionary.bindingSource;
            this.dataGridViewSortCategoryList.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dataGridViewSortCategoryList.MergeColumnNames.Add("CategoryValue");
            this.dataGridViewSortCategoryList.ClearSelection();
            return pageData.TotalCount;

        }
        private int pageControlDictionary_EventPaging(ElectronicWMSA.Comm.Forms.MsControls.EventPagingArg e)
        {
            return DBindDictionary();
        }
        #endregion

        #region 绑定分类
        public void DBindSort()
        {
            DataSet ds = new DataSet();
            ds = bll_Datset.GetDataSet("TDictionary_Sort", "");
            this.comboBoxDictionary_Sort.DataSource = ds.Tables[0].DefaultView;
            this.comboBoxDictionary_Sort.ValueMember = "ID";
            this.comboBoxDictionary_Sort.DisplayMember = "Value";
        }
        #endregion

        #region Sort 分类
        /// 新增
        private void btnSortAdd_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("新增")) return;

            string sortValue = txtSortValue.Text.Trim();
            if (sortValue == "")
            {
                this.labShowSort.Text = "分类名称不能为空！";
                return;
            }
            if (bll_Sort.GetEntityByName_Dictionary_Sort(sortValue) != null)
            {
                this.labShowSort.Text = "分类名称已存在！";
                return;
            }
            if (MessageClass.enquire("确认添加新信息！") == DialogResult.OK)
            {
                if (bll_Sort.Add_Sort(sortValue))
                {
                    this.DBind();
                    this.pageControlDictionary.Bind();
                }
                else
                {
                    MessageClass.error("失败！");
                    this.labShowSort.Text = "失败！";
                }
            }
        }

        /// 编辑
        private void btnSortUpdate_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("编辑")) return;

            string sortValue = txtSortValue.Text.Trim();
            if (_sortid == 0)
            {
                this.labShowSort.Text = "请选择信息！";
                return;
            }
            if (sortValue == "")
            {
                this.labShowSort.Text = "分类名称不能为空！";
                return;
            }
            if (MessageClass.enquire("确认修改新信息！") == DialogResult.OK)
            {
                TDictionary_SortEntity entity = new TDictionary_SortEntity();
                entity.ID = _sortid;
                entity.Value = sortValue;
                if (bll_Sort.Update_Sort(entity))
                {
                    MessageClass.right("成功！");
                    this.DBind();
                    this.pageControlDictionary.Bind();
                }
                else
                {
                    MessageClass.error("失败！");
                    this.labShowSort.Text = "失败！";
                }
            }
        }
        #endregion

        #region Category类别
        /// 新增
        private void btnCategoryAdd_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("新增")) return;
            int sortid = Convert.ToInt32(comboBoxDictionary_Sort.SelectedValue);
            string categoryValue = txtCategoryValue.Text.Trim();
            if (categoryValue == "")
            {
                this.labShowCategory.Text = "类别名称不能为空！";
                return;
            }
            if (bll_Category.GetEntityByName_Dictionary_Category(sortid, categoryValue) != null)
            {
                this.labShowCategory.Text = "同一分类下类别名称已存在！";
                return;
            }
            if (MessageClass.enquire("确认添加新信息！") == DialogResult.OK)
            {
                if (bll_Category.Add_Category(sortid, categoryValue))
                {
                    this.pageControlDictionary.Bind();
                }
                else
                {
                    MessageClass.error("失败！");
                    this.labShowCategory.Text = "失败！";
                }
            }
        }

        /// 编辑
        private void btnCategoryUpdate_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("编辑")) return;

            int sortid = Convert.ToInt32(comboBoxDictionary_Sort.SelectedValue);
            string categoryValue = txtCategoryValue.Text.Trim();
            if (_sortid == 0)
            {
                this.labShowCategory.Text = "请选择信息！";
                return;
            }
            if (categoryValue == "")
            {
                this.labShowCategory.Text = "类别名称不能为空！";
                return;
            }
            if (MessageClass.enquire("确认修改信息！") == DialogResult.OK)
            {
                TDictionary_CategoryEntity entity = new TDictionary_CategoryEntity();
                entity.ID = _categoryid;
                entity.SortID = sortid;
                entity.Value = categoryValue;
                if (bll_Category.Update_Dictionary_Category(entity))
                {
                    MessageClass.right("成功！");
                    this.DBind();
                    this.pageControlDictionary.Bind();
                }
                else
                {
                    MessageClass.error("失败！");
                    this.txtCategoryValue.Text = "失败！";
                }
            }
        }
        #endregion

        #region 事件
        TPermissions_BLL bll_TPermissions = new TPermissions_BLL();
        private void dataGridViewSortCategoryList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                if (dataGridViewSortCategoryList.Rows.Count > 0)
                {
                    #region 获取单列详细信息
                    if (e.ColumnIndex > -1 && dataGridViewSortCategoryList.CurrentCell.ColumnIndex == 0)
                    {
                        _sortid = Convert.ToInt32(dataGridViewSortCategoryList.CurrentRow.Cells["SortID"].Value.ToString());
                        _categoryid = Convert.ToInt32(dataGridViewSortCategoryList.CurrentRow.Cells["ID"].Value.ToString());
                        TDictionary_SortEntity entity_Sort = new TDictionary_SortEntity();
                        TDictionary_CategoryEntity entity_Category = new TDictionary_CategoryEntity();
                        entity_Sort = bll_Sort.GetEntityByID_Dictionary_Sort(_sortid);
                        entity_Category = bll_Category.GetEntityByID_Dictionary_Category(_categoryid);
                        if (entity_Sort != null)
                            this.txtSortValue.Text = entity_Sort.Value;
                        else
                            this.txtSortValue.Text = "";
                        if (entity_Category != null)
                        {
                            this.comboBoxDictionary_Sort.SelectedValue = entity_Sort.ID;
                            this.txtCategoryValue.Text = entity_Category.Value;
                        }
                        else
                        {
                            this.txtCategoryValue.Text = "";
                        }
                    }
                    #endregion

                    #region 确认删除
                    if (dataGridViewSortCategoryList.CurrentCell.ColumnIndex == 1)
                    {
                        if (!SystemVariable.VaildationPermission("删除")) return;

                        int sortID = Convert.ToInt32(dataGridViewSortCategoryList.CurrentRow.Cells["SortID"].Value.ToString());
                        int id = Convert.ToInt32(dataGridViewSortCategoryList.CurrentRow.Cells["ID"].Value.ToString());
                        this._sortid = sortID;
                        this._categoryid = id;

                        if (_categoryid != 0)
                        {
                            if (!bll_TPermissions.ExistsByType(_categoryid))
                            {
                                if (MessageClass.enquire("确认删除信息！") == DialogResult.OK)
                                {
                                    if (bll_Category.Del_Dictionary_Category(_categoryid))
                                    {
                                        MessageClass.right("成功！");
                                        this.pageControlDictionary.Bind();
                                    }
                                    else
                                    {
                                        MessageClass.error("失败！");
                                        this.labShowCategory.Text = "失败！";
                                    }
                                }
                            }
                            else
                            {
                                MessageClass.error("已调用，不可删除！");
                            }
                        }
                        else
                        {
                            if (_sortid != 0)
                            {
                                if (MessageClass.enquire("确认删除信息！") == DialogResult.OK)
                                {
                                    if (bll_Sort.Del_Dictionary_Sort(_sortid))
                                    {
                                        MessageClass.right("成功！");
                                        this.DBind();
                                        this.pageControlDictionary.Bind();
                                    }
                                    else
                                    {
                                        MessageClass.error("失败！");
                                        this.labShowSort.Text = "失败！";
                                    }
                                }
                            }
                        }
                    }
                    #endregion
                }
            }
        }
        #endregion

        #region 序号
        private void dataGridViewSortCategoryList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(dataGridViewSortCategoryList.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dataGridViewSortCategoryList.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        #endregion

        #region 查询
        private void btnSearchDictionary_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("查询")) return;

            string category = txtSearchCategory.Text.Trim();
            string sort = txtSearch.Text.Trim();

            strWhere = "1=1 AND CategoryValue like '%" + category + "%'And SortValue like '%" + sort + "%'";
            this.pageControlDictionary.Bind();
        }
        #endregion

        #region 重置
        private void btnSortClear_Click(object sender, EventArgs e)
        {
            this.txtSortValue.Text = "";
            this._sortid = 0;
        }
        private void btnCategoryClear_Click(object sender, EventArgs e)
        {
            this.txtCategoryValue.Text = "";
            this._categoryid = 0;
        }
        #endregion

        #region 退出
        private void linkLabelClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPages();
        }

        private void linkLabellinkLabelCloseAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseAllTabPages();
        }

        private void linkLabelCloseElse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPageElse();
        }
        #endregion
    }
}
